package com.learn.hibernatevalidator.chapter02.classlevel;

import jakarta.validation.Constraint;
import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;
import jakarta.validation.Payload;

import java.lang.annotation.*;

/**
 * @author: liyf
 * @create: 2024-12-21 21:31
 **/
@Documented
@Constraint(validatedBy = {ValidPassengerCount.Validator.class})
@Target({ElementType.ANNOTATION_TYPE, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface ValidPassengerCount {

    String message() default "{org.hibernate.validator.referenceguide.chapter02.classlevel.ValidPassengerCount.message}";

    Class<?>[] groups() default {};

    Class<? extends Payload>[] payload() default {};

    class Validator implements ConstraintValidator<ValidPassengerCount, Car> {

        @Override
        public void initialize(ValidPassengerCount constraintAnnotation) {
            ConstraintValidator.super.initialize(constraintAnnotation);
        }

        @Override
        public boolean isValid(Car car, ConstraintValidatorContext constraintValidatorContext) {
            return car.getPassengers().size() <= car.getSeatCount();
        }
    }

}
